<template>
<Card style="height:800px">
    <Row :gutter="16">
        <Col span="5">
        <Button type="text" style="font-family:'PingFang SC';font-weight:bold;font-size:16px;">组织机构</Button>
        <Tree :data="treeData" @on-select-change="handleSelectChange"></Tree>
        </Col>
        <Col span="5">
        <Button type="text" style="font-family:'PingFang SC';font-weight:bold;font-size:16px;color:#57a3f3;" @click="triggerAddRootNode">岗位</Button>
        <Tree :data="treeData2" @on-select-change="handlePositionChange"></Tree>
        </Col>
        <Col span="14">
        <Button type="text" style="font-family:'PingFang SC';font-weight:bold;font-size:16px;">岗位：{{form.positionCode}}-{{form.positionName}}</Button>
        <div v-if="this.subVue === 'edit'">
            <Form ref="form" :model="form" :rules="rules" :label-width="80">
                <FormItem label="岗位代码" prop="positionCode">
                    <Input v-model="form.positionCode" placeholder="" :disabled="type===2||type===0">
                    </Input>
                </FormItem>
                <FormItem label="岗位名称" prop="positionName">
                    <Input v-model="form.positionName" placeholder="" :disabled="type===0">
                    </Input>
                </FormItem>
                <FormItem label="上级岗位" prop="parentPositionName">
                    <Input v-model="form.parentPositionName" disabled placeholder="">
                    </Input>
                </FormItem>
                <FormItem label="机构名称" prop="orgName">
                    <Input v-model="form.orgName" disabled placeholder="">
                    </Input>
                </FormItem>
                <FormItem label="岗位描述" prop="description">
                    <Input v-model="form.description" type="textarea" :autosize="{minRows: 2,maxRows: 5}" placeholder="" :disabled="type===0">
                    </Input>
                </FormItem>
                <FormItem v-if="this.type===1 || this.type ===2 || this.type===4">
                    <Button type="primary" @click="handleSubmit">确定</Button>
                    <Button type="" style="margin-left: 8px" @click="handleBack">返回</Button>
                </FormItem>
                <FormItem v-else>
                    <Button type="primary" @click="handleEditor">编辑</Button>
                    <Button type="primary" @click="handleAdd" style="margin-left: 15px;">新建</Button>
                    <Button type="error" @click="handleDelete" style="margin-left: 15px;">删除</Button>
                </FormItem>
            </Form>
        </div>
        </Col>
    </Row>
</Card>
</template>

<script>
export default {
  data () {
    return {
      baseUrl: this.$appContext.marvels_core_system,
      $ajax: null,
      treeData: [], // 组织机构树
      treeData2: [], // 岗位树
      selectOrgObj: null,
      selectPositionObj: null,
      selectPositionCopy: null,
      subVue: '',
      type: null, // 0 查看 1 新建子节点 4新建根节点 2 修改
      form: {
        positionCode: null,
        positionName: null,
        description: null,
        parentPositionName: null,
        orgName: null
      },
      rules: {
        positionCode: [{
          required: true,
          message: '岗位代码不能为空',
          trigger: 'blur'
        }],
        positionName: [{
          required: true,
          message: '岗位名称不能为空',
          trigger: 'blur'
        }]
      }
    }
  },
  methods: {
    initpage: function () {
      var vm = this
      this.$ajax.post(this.baseUrl + 'system/pmsn/service/getOrgTree').then(re => {
        var data = re.data
        var code = data.code
        var message = data.message
        if (code !== 'success') {
          this.$Message.warning(message)
          return
        }
        vm.treeData = vm.toTree(data.data)
      }).catch(er => {

      })
    },
    toTree: function (data) {
      var vm = this
      if (data && data.length > 0) {
        for (var index in data) {
          var onedata = data[index]
          data[index].title = onedata.orgName
          if (data[index].children && data[index].children.length > 0) {
            vm.toTree(data[index].children)
          }
        }
      }
      return data
    },
    // 组织机构树节点点击事件
    handleSelectChange (val) {
      var vm = this
      var params = {}
      params = {
        orgCode: val[0].orgCode
      }
      this.selectOrgObj = val[0]
      this.$ajax.post(this.baseUrl + 'system/pmsn/service/getPositionTreeByOrgCode', params).then(re => {
        var data = re.data
        var code = data.code
        var message = data.message
        if (code !== 'success') {
          this.$Message.warning(message)
          return
        }
        vm.treeData2 = vm.toPositionTree(data.data)
      }).catch(er => {

      })
    },
    toPositionTree: function (data) {
      var vm = this
      if (data && data.length > 0) {
        for (var index in data) {
          var onedata = data[index]
          data[index].title = onedata.positionName
          vm.toPositionTree(data[index].children)
        }
      }
      return data
    },
    // 新增岗位根节点
    triggerAddRootNode () {
      if (this.selectOrgObj === null) {
        this.$Message.warning('请先选择一个组织机构')
        return
      }
      this.form = {}
      this.form.orgCode = this.selectOrgObj.orgCode
      this.form.orgName = this.selectOrgObj.orgName
      this.subVue = 'edit'
      this.type = 4
    },
    // 岗位树节点点击事件
    handlePositionChange (val) {
      this.selectPositionObj = val[0]
      this.subVue = 'edit'
      this.type = 0
      this.showPositionInfo()
    },
    // 展示岗位节点详情
    showPositionInfo () {
      var position = {
        positionId: this.selectPositionObj.positionId
      }
      this.$ajax.post(this.baseUrl + 'system/pmsn/service/positionDetail', position).then(re => {
        var data = re.data
        var code = data.code
        var message = data.message
        if (code !== 'success') {
          this.$Message.warning(message)
          return
        }
        var obj = data.data
        this.form = obj
        this.selectPositionObj = obj
        this.selectPositionCopy = JSON.parse(JSON.stringify(obj))
      }).catch(er => {

      })
    },
    handleSubmit () {
      var vm = this
      this.$refs.form.validate(valid => {
        if (valid) {
          var config = {
            title: '提示'
          }
          if (this.type === 2) {
            config.content = '是否确认修改?'
            config.onOk = vm.updatePosition
          } else if (this.type === 1) {
            config.content = '是否确认新建?'
            config.onOk = vm.addPosition
          } else if (this.type === 4) {
            config.content = '是否确认新建?'
            config.onOk = vm.addRootPosition
          }
          vm.$Modal.confirm(config)
        }
      })
    },
    addRootPosition: function () {
      var vm = this
      var position = this.form
      this.$ajax.post(this.baseUrl + 'system/pmsn/service/addRootPosition', position).then(re => {
        var data = re.data
        var code = data.code
        var message = data.message
        if (code !== 'success') {
          this.$Message.warning(message)
          return
        }
        vm.form = data.data
        vm.refreshTree(data.data)
        this.type = 0
      }).catch(er => {

      })
    },
    handleBack () {
      // 0 查看 1 新建子节点 4新建根节点 2 修改 3 删除
      if (this.type === 1) {
        this.form = this.selectPositionObj
      }
      this.type = 0
    },
    handleAdd () {
      this.type = 1
      this.form = {}
      this.form = {
        parentPositionName: this.selectPositionObj.positionName,
        parentPositionCode: this.selectPositionObj.positionCode,
        orgCode: this.selectOrgObj.orgCode,
        orgName: this.selectOrgObj.orgName
      }
    },
    handleEditor () {
      this.type = 2
    },
    handleDelete () {
      var vm = this
      this.type = 3
      var position = this.selectPositionObj
      if (position.positionId) {
        this.$Modal.confirm({
          content: '确认删除该节点【' + position.positionName + '】及其子节点吗？',
          onOk: function () {
            vm.$ajax.post(vm.baseUrl + 'system/pmsn/service/delPosition', position).then(re => {
              var data = re.data
              var code = data.code
              var message = data.message
              if (code !== 'success') {
                this.$Message.warning(message)
                return
              }
              vm.subVue = ''
              vm.form = {}
              vm.refreshTree(vm.treeData2)
            }).catch(er => {

            })
          }
        })
      }
    },
    updatePosition () {
      var vm = this
      var params = this.form
      this.$ajax.post(this.baseUrl + 'system/pmsn/service/updateByPositionId', params).then(re => {
        var data = re.data
        var code = data.code
        var message = data.message
        if (code !== 'success') {
          this.$Message.warning(message)
          return
        }
        vm.selectPositionObj = vm.form
        vm.refreshTree(vm.treeData2)
        this.type = 0
      }).catch(er => {

      })
    },
    addPosition () {
      var vm = this
      var position = this.form
      this.$ajax.post(this.baseUrl + 'system/pmsn/service/addPosition', position).then(re => {
        var data = re.data
        var code = data.code
        var message = data.message
        if (code !== 'success') {
          this.$Message.warning(message)
          return
        }
        vm.form = this.selectPositionObj
        vm.refreshTree(vm.treeData2, data.data)
        this.type = 0
      }).catch(er => {

      })
    },
    // 刷新岗位树
    refreshTree: function (listTree, subNode) {
      if (this.type === 3) { // 删除
        if (listTree && listTree.length > 0) {
          for (var i in listTree) {
            if (listTree[i].positionCode === this.selectPositionObj.positionCode) {
              listTree.splice(i, 1)
              return
            }
            if (listTree[i].children && listTree[i].children.length > 0) {
              this.refreshTree(listTree[i].children)
            }
          }
        }
      } else if (this.type === 4) { // 新增根节点
        var newRoot = listTree
        newRoot.title = newRoot.positionName
        this.treeData2.push(newRoot)
      } else if (this.type === 1) { // 新增子节点
        if (listTree && listTree.length > 0) {
          for (var it1 in listTree) {
            if (listTree[it1].positionCode === this.selectPositionObj.positionCode) {
              subNode.title = subNode.positionName
              if (!listTree[it1].children || listTree[it1].children.length < 1) {
                listTree[it1].children = []
              }
              listTree[it1].children.push(subNode)
              return
            }
            if (listTree[it1].children && listTree[it1].children.length > 0) {
              this.refreshTree(listTree[it1].children, subNode)
            }
          }
        }
      } else if (this.type === 2) { // 更新节点
        if (listTree && listTree.length > 0) {
          for (var it in listTree) {
            if (listTree[it].positionCode === this.selectPositionObj.positionCode) {
              listTree[it].title = this.selectPositionObj.positionName
              return
            }
            if (listTree[it].children && listTree[v].children.length > 0) {
              this.refreshTree(listTree[it].children)
            }
          }
        }
      }
    }
  },
  mounted () {
    this.$ajax = this.$util.ajax()
    this.initpage()
  },
  created: function () {

  }
}
</script>
